/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef OHMW_FRAMEWORK_CXX_BINCODE_TO_RON_H
#define OHMW_FRAMEWORK_CXX_BINCODE_TO_RON_H

#include <cstdint>
#include <iomanip>
#include <ostream>

namespace OHOS {
namespace OHMW {

inline std::ostream& ToRon(std::ostream& os, bool value) {
    os << (value ? "true" : "false");
    return os;
}

inline std::ostream& ToRon(std::ostream& os, char value) {
    os << static_cast<int>(value);
    return os;
}

inline std::ostream& ToRon(std::ostream& os, uint8_t value) {
    os << static_cast<int>(value);
    return os;
}

inline std::ostream& ToRon(std::ostream& os, uint16_t value) {
    os << value;
    return os;
}

inline std::ostream& ToRon(std::ostream& os, uint32_t value) {
    os << value;
    return os;
}

inline std::ostream& ToRon(std::ostream& os, uint64_t value) {
    os << value;
    return os;
}

inline std::ostream& ToRon(std::ostream& os, int8_t value) {
    os << static_cast<int>(value);
    return os;
}

inline std::ostream& ToRon(std::ostream& os, int16_t value) {
    os << value;
    return os;
}

inline std::ostream& ToRon(std::ostream& os, int32_t value) {
    os << value;
    return os;
}

inline std::ostream& ToRon(std::ostream& os, int64_t value) {
    os << value;
    return os;
}

inline std::ostream& ToRon(std::ostream& os, float value) {
    if (value == static_cast<int32_t>(value)) {
        std::ios::fmtflags f = os.flags();
        std::streamsize p = os.precision();

        os << std::fixed << std::setprecision(1) << value;

        os.flags(f);
        os.precision(p);
    } else {
        std::ios::fmtflags f = os.flags();
        std::streamsize p = os.precision();

        os << std::setprecision(6) << value;

        os.flags(f);
        os.precision(p);
    }
    return os;
}

inline std::ostream& ToRon(std::ostream& os, double value) {
    if (value == static_cast<int64_t>(value)) {
        std::ios::fmtflags f = os.flags();
        std::streamsize p = os.precision();

        os << std::fixed << std::setprecision(1) << value;

        os.flags(f);
        os.precision(p);
    } else {
        std::ios::fmtflags f = os.flags();
        std::streamsize p = os.precision();

        os << std::setprecision(15) << value;

        os.flags(f);
        os.precision(p);
    }
    return os;
}

inline std::ostream& ToRon(std::ostream& os, std::string_view s) {
    os << '"' << s << '"';
    return os;
}

inline std::ostream& ToRon(std::ostream& os, const std::string& s) {
    os << '"' << s << '"';
    return os;
}
}
}

#endif
